﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.GuessTheWordsForWP7.Core;
using Microsoft.Phone.Tasks;
using System.Windows.Threading;

namespace GuessTheWordsForWP7
{
    public partial class MainPage : PhoneApplicationPage
    {

        private static bool isShowOpenAd = false;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(MainPage_Loaded);
            string fromloading = "";
            NavigationContext.QueryString.TryGetValue("fromloading", out fromloading);
            if (fromloading == "1")
            {
                NavigationService.RemoveBackEntry();
            }

            this.btnStartGame.Click += new RoutedEventHandler(btnStartGame_Click);
            //this.btnTopList.Click += new RoutedEventHandler(btnTopList_Click);
            this.btnContinue.Click += new RoutedEventHandler(btnContinue_Click);

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    string dateStr = DateTime.Now.ToString("yyyyMMdd") + (DateTime.Now.Hour < 12 ? "A" : "P");
                    if (StageManager.Instance.LastFullScreenDate != dateStr)
                    {
                        StageManager.Instance.LastFullScreenDate = dateStr;
                        NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=fktz", UriKind.RelativeOrAbsolute));
                    }
                };
                timer.Start();

            }

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (UmengSDK.UmengAnalytics.GetOnlineParam("useGift") == "1")
            {
                this.btnGift.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void btnContinue_Click(object sender, RoutedEventArgs e)
        {
            if (StageManager.Instance.LastStage > 0)
            {
                NavigationService.Navigate(new Uri("/GamePage.xaml?stageIndex=" + StageManager.Instance.LastStage, UriKind.Relative));
            }
            else
            {
                MessageBox.Show("没有保存的关卡数据");
            }
        }

        void btnTopList_Click(object sender, RoutedEventArgs e)
        {
            //if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            //{
            //    OpenXLive.Silverlight.XLiveSLFormManager.ShowGameCenter(this);
            //}
        }

        void btnStartGame_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/StagePage.xaml", UriKind.Relative));
        }

        bool isOnGift = false;

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnGift)
            {
                this.StoryboardHidePackage.Begin();
                isOnGift = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void btnGift_Click_1(object sender, RoutedEventArgs e)
        {
            isOnGift = true;
            this.StoryboardShowPackage.Begin();
        }

        private void btnGetMB_Click_1(object sender, RoutedEventArgs e)
        {
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加50
            StageManager.Instance.LampCount += 50;

            this.btnGetMB.Content = "已领取";
            this.btnGetMB.IsEnabled = false;
            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnCloseGift_Click_1(object sender, RoutedEventArgs e)
        {
            isOnGift = false;
            this.StoryboardHidePackage.Begin();
        }
    }
}